# -*- coding: cp1252 -*-
from wx import *
from wx.lib.rcsizer import RowColSizer
import manageperson
import manageservice
import manageposte
import manageprojet
import manageformation
import co
import re

PERSONNES=wx.NewId()
SERVICES=wx.NewId()
FORMATIONS=wx.NewId()
POSTES=wx.NewId()
PROJETS=wx.NewId()
DISPLAY=wx.NewId()
VALIDE=wx.NewId()
ANNULE=wx.NewId()
#LOGS=wx.NewId()
CLOSE=wx.NewId()
#LOGOUT ?


class main(wx.Dialog):
    def __init__(self,parent,id,title, **kwds):
        #begin wxGlade: manage_users.__init__
        self.connect=co.co()
        self.idu = 0
        self.title = "Gestion du personnel"
        self.parent=parent
        kwds["style"] = (wx.DEFAULT_DIALOG_STYLE|wx.MINIMIZE_BOX|
                         wx.TAB_TRAVERSAL)#wx.RESIZE_BORDER
        wx.Dialog.__init__(self, parent,id,title, **kwds)

        self.lctsup = wx.ListCtrl(self, DISPLAY, style=wx.LC_REPORT|wx.LC_SINGLE_SEL, size=(1,150))

    #------------------------COLONNES----------------------------

        self.lctsup.InsertColumn(0,'id')
        self.lctsup.InsertColumn(1,'Table')
        self.lctsup.InsertColumn(2,'Quoi')
        self.lctsup.InsertColumn(3,'Quand')
        self.lctsup.InsertColumn(4,'Action')
        
     #------------------------BOUTONS------------------------------
        self.butpers = wx.Button(self, PERSONNES, "Personnel")
        self.butserv = wx.Button(self, SERVICES, "Services")
        self.butform = wx.Button(self, FORMATIONS, "Formations")
        self.butpost = wx.Button(self, POSTES, "Postes")
        self.butproj = wx.Button(self, PROJETS, "Projets")
        #self.butlogs = wx.Button(self, LOGS, "Logs")
        self.butval = wx.Button(self, VALIDE, "V")
        self.butdel = wx.Button(self,ANNULE, "X")
        self.butclose = wx.Button(self, CLOSE, "Quitter")

    #----------------------- Appel Interface ----------------------
        self.__do_layout()




    def __do_layout(self):
        #boxa = all
        #boxt = tableau
        #boxb = boutons
        #boxv = validation ou non des logs
        #taille = wx.Size(500,215)
        #boxa = RowColSizer()
        #boxa.SetMinSize(taille)
        boxa = wx.BoxSizer(wx.VERTICAL)
        boxt = wx.BoxSizer(wx.VERTICAL)
        boxb = wx.BoxSizer(wx.HORIZONTAL)
        boxv = wx.BoxSizer(wx.HORIZONTAL)
        boxt.Add(self.lctsup, 1, wx.EXPAND)
        boxb.Add(self.butpers, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        boxb.Add(self.butserv, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        boxb.Add(self.butform, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        boxb.Add(self.butpost, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        boxb.Add(self.butproj, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        #boxb.Add(self.butlogs, 0,wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT)
        boxb.Add(self.butclose, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        boxv.Add(self.butval, 1,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        boxv.Add(self.butdel, 1,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        boxt.Add(boxv,0,wx.EXPAND)
        boxa.Add(boxb,1,wx.EXPAND)
        boxa.Add(boxt,0,wx.EXPAND)
        self.SetAutoLayout(1)
        self.SetSizer(boxa)
        boxa.SetSizeHints(self)
        self.Layout()
        wx.EVT_BUTTON(self, VALIDE, self.valide)
        wx.EVT_BUTTON(self, ANNULE, self.annule)
        wx.EVT_BUTTON(self, PERSONNES, self.pers)
        wx.EVT_BUTTON(self, SERVICES, self.serv)
        wx.EVT_BUTTON(self, POSTES, self.post)
        wx.EVT_LIST_ITEM_SELECTED(self, DISPLAY, self.display)
        wx.EVT_BUTTON(self, FORMATIONS, self.form)
        wx.EVT_BUTTON(self, CLOSE, self.close)
        wx.EVT_BUTTON(self, PROJETS, self.proj)
        print self.getLogListe()
        self.refresh()
        
    def refresh(self):
        self.lctsup.DeleteAllItems()
        x = 0
        self.logList = self.getLogListe() #external program
        for k in self.logList:
            self.lctsup.InsertStringItem(x,k[0])
            self.lctsup.SetStringItem(x,1,k[1])
            self.lctsup.SetStringItem(x,2,k[2])
            self.lctsup.SetStringItem(x,3,k[3])
            self.lctsup.SetStringItem(x,4,k[4])
            x += 1
            
    def getLogListe(self):
        tab=[]
        tab1=[]
        fichier = open("../log.xml","r")
        for lignes in fichier:
            res = re.search("<.*>(.*)</.*>",lignes)
            if res:
                tab1.append(res.group(1))
            if re.search("</requette>",lignes):
                tab.append(tab1)
                tab1=[]
        fichier.close()
        return tab
    
    def valide(self,event):
        self.connect.executeReq(self.idlog)
        self.deleteFromFile(self.iddellog)
        self.refresh()

    def annule(self,event):
        if self.iddellog:
            mg = "Are You Sure You want to delete ?"
            msg = wx.MessageDialog(self,mg,"Warning",wx.OK|wx.CANCEL)
            res = msg.ShowModal()
            msg.Destroy()
            if res == wx.ID_OK:
                self.deleteFromFile(self.iddellog)
                self.refresh()
            else:
                self.refresh()
        

    def deleteFromFile(self,identifiant):
        enlever=[]
        fichier = open("../log.xml","r")
        toutesleslignes = fichier.readlines()
        for i in range(len(toutesleslignes)):
            res = re.search("(<id>"+identifiant+"</id>)",toutesleslignes[i])
            if res:
                enlever.append(i)
        for i in range(7):
            print toutesleslignes[enlever[0]-1]
            toutesleslignes.pop(enlever[0]-1)
        garder = toutesleslignes
        fichier.close()
        fichier = open("../log.xml","w")
        for i in garder:
            fichier.write(i)
        fichier.close()
        
        
    def close(self, event):
        event
        self.connect.fermerConnections(self)
        self.Destroy()

    def pers(self, event):
        persframe=manageperson.managepersons(self,-1,'Gestion du personnel')
        persframe.Show()
        
    def serv(self, event):
        servframe=manageservice.manageservices(self,-1,'Gestion des services')
        servframe.Show()
        
    def form(self, event):
        formframe=manageformation.manageformations(self,-1,'Gestion des formations')
        formframe.Show()
        
    def post(self, event):
        postframe=manageposte.managepostes(self,-1,'Gestion des postes')
        postframe.Show()
        
    def proj(self, event):
        projframe=manageprojet.manageprojets(self,-1,'Gestion des projets')
        projframe.Show()

    def display(self,event):
        curitem = event.m_itemIndex
        self.idlog = self.lctsup.GetItem(curitem,4).GetText()
        self.iddellog = self.lctsup.GetItem(curitem,0).GetText()
        
class app(wx.App):
    def OnInit(self):
        frame=main(None,-1,'Projet POO')
        self.SetTopWindow(frame)
        frame.Show()
        return 1

if __name__ == "__main__":
    prog = app(0)
    prog.MainLoop()
